package com.daikting.bloodtest;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.daikting.bloodservice.BloodActivity;

import java.util.Random;

public class MainActivity extends BloodActivity implements OnClickListener, BloodActivity.OnStateListener {
    private Sphygmomanometer sphygmomanometer;
    private Context mContext = MainActivity.this;
    Button btnStart, btnStop;
    TextView maximumPressure, minimumPressure, heartRate,heartState;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        sphygmomanometer = (Sphygmomanometer) findViewById(R.id.sphygmomanometer);
        btnStart = (Button) findViewById(R.id.btnStart);
        btnStart.setOnClickListener(this);
        btnStop = (Button) findViewById(R.id.btnStop);
        btnStop.setOnClickListener(this);
        maximumPressure = (TextView) findViewById(R.id.maximumPressure);
        minimumPressure = (TextView) findViewById(R.id.minimumPressure);
        heartRate = (TextView) findViewById(R.id.heartRate);
        heartState = (TextView) findViewById(R.id.heartState);
        maximumPressure.setText("高压：0bpm");
        minimumPressure.setText("低压：0bpm");
        heartRate.setText("心率：0次/分钟");
        heartState.setText("");
        setOnStateListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btnStart:
                maximumPressure.setText("高压：0bpm");
                minimumPressure.setText("低压：0bpm");
                heartRate.setText("心率：0次/分钟");
                heartState.setText("");
                checkState();
                break;
            case R.id.btnStop:
                drawHandler.removeCallbacks(runnable);
                stopTest();
                break;
        }
    }

    Handler drawHandler = new Handler();
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            sphygmomanometer.setTargetTemperature(random());
            drawHandler.postDelayed(this, 200);
        }
    };


    private int random() {
        int max = 300;
        int min = 10;
        Random random = new Random();
        int s = random.nextInt(max) % (max - min + 1) + min;
        return s;
    }

    @Override
    public void onStateListener(State state, ResultBean resultBean) {
        switch (state) {
            case CheckFailed:
                Toast.makeText(mContext, "设备检测失败！", Toast.LENGTH_SHORT).show();
                break;
            case CheckSuccess:
                Toast.makeText(mContext, "设备检测成功！", Toast.LENGTH_SHORT).show();
                requestAuth();
                break;
            case AuthFailed:
                Toast.makeText(mContext, "需要授权后才能使用！", Toast.LENGTH_SHORT).show();
                break;
            case AuthSuccess:
                Toast.makeText(mContext, "授权成功,开始测试！！", Toast.LENGTH_SHORT).show();
                startTest();
                drawHandler.postDelayed(runnable, 3000);

                break;
            case Disconnected:
                Toast.makeText(mContext, "设备连接已断开，请检查！", Toast.LENGTH_SHORT).show();
                break;
            case TestStart:
                Toast.makeText(mContext, "开始测试，请保持安静！", Toast.LENGTH_SHORT).show();
                break;
            case TestSuccess:
                Toast.makeText(mContext, "测试完成！", Toast.LENGTH_SHORT).show();

                if (resultBean != null) {
                    String result = "高压：" + resultBean.getMaximumPressure() +
                            "低压：" + resultBean.getMinimumPressure() +
                            "心率：" + resultBean.getHeartRate();
                    maximumPressure.setText("高压：" + resultBean.getMaximumPressure() + "bpm");
                    minimumPressure.setText("低压：" + resultBean.getMinimumPressure() + "bpm");
                    heartRate.setText("心率：" + resultBean.getHeartRate() + "次/分钟");
                    heartState.setText(""+resultBean.getState());
                }
                drawHandler.removeCallbacks(runnable);
                sphygmomanometer.setTargetTemperature(0);
                break;
            case TestFailed:
                Toast.makeText(mContext, "测试失败！", Toast.LENGTH_SHORT).show();

                break;
        }
    }
}